package com.example.zg3_park.CarType

import android.view.Gravity
import android.view.LayoutInflater
import android.view.ViewGroup.LayoutParams
import android.widget.PopupWindow
import android.widget.RadioButton
import android.widget.RadioGroup
import android.widget.SearchView
import android.widget.SearchView.OnQueryTextListener
import android.widget.TextView
import androidx.lifecycle.lifecycleScope
import androidx.paging.PagingData
import androidx.paging.filter
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.lib_base.entity.CarTypeEntityItem
import com.example.lib_base.mvvm.BaseActivity
import com.example.zg3_park.R
import com.example.zg3_park.databinding.ActivityCarTypeBinding

import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import org.w3c.dom.Text

@AndroidEntryPoint
@Route(path = "/app/CarTypeActivity")
class CarTypeActivity : BaseActivity<ActivityCarTypeBinding,CarTypeViewModel>() {
    private val carTypeAdapter by lazy { CarTypeAdapter() }
    var para = ""
    var areaId = -1
    //默认为 类型
    var type = "类型"
    //数据源
    lateinit var datas:PagingData<CarTypeEntityItem>

    override fun intiView() {
        binding.rvCarType.apply {
            adapter = carTypeAdapter
            layoutManager = LinearLayoutManager(this@CarTypeActivity)
        }

        //跳转添加车位
        binding.ivAdd.setOnClickListener {
            ARouter.getInstance().build("/app/AddParkActivity").navigation()
        }

        binding.ivBack.setOnClickListener {
            //ARouter.getInstance().build("/app/MainActivity").navigation()
            finish()
        }

        lifecycleScope.launch {
            vm.getCarType("标准车位",type).collect() {
                datas = it
                carTypeAdapter.submitData(it)
            }

        }

        //弹框
        binding.ivShai.setOnClickListener {
            val popupWindow = PopupWindow()
            popupWindow.isFocusable = true
            popupWindow.width = LayoutParams.MATCH_PARENT
            popupWindow.height = LayoutParams.WRAP_CONTENT
            popupWindow.isOutsideTouchable = true
            val view = LayoutInflater.from(this).inflate(R.layout.item_pop_cartype, null)
            val rb1 = view.findViewById<RadioGroup>(R.id.rb1)
            rb1.setOnCheckedChangeListener { radioGroup, i ->
                when(i) {
                    R.id.rb_biaozhun -> {
                        para = "标准车位"
                    }
                    R.id.rb_vip -> {
                        para = "VIP车位"
                    }
                }
                lifecycleScope.launch {
                    vm.getCarType(para,"类型").collect{
                        carTypeAdapter.submitData(it)
                    }
                }
            }

            val rb2 = view.findViewById<RadioGroup>(R.id.rb2)
            rb2.setOnCheckedChangeListener { radioGroup, i ->
                type = "状态"
                var para = ""
                when(i) {
                    R.id.rb_kongxian ->
                        para = "空闲"
                    R.id.rb_zhanyong ->
                        para = "已占用"
                    else ->
                        para = "维护中"
                }
                lifecycleScope.launch {
                    vm.getCarType(para,"状态").collect{
                        carTypeAdapter.submitData(it)
                    }
                }
            }
//            val rb3 = view.findViewById<RadioGroup>(R.id.rb3)
//            rb3.setOnCheckedChangeListener { radioGroup, i ->
//                when(i) {
//                    R.id.rb_1 ->
//                        areaId = 1
//                    R.id.rb_2 ->
//                        areaId = 2
//                    R.id.rb_3 ->
//                        areaId = 3
//                    else ->
//                        areaId = 4
//                }
//
//                lifecycleScope.launch {
//                    vm.getCarType(areaId.toString(),"区域").collect() {
//                        carTypeAdapter.submitData(it)
//                    }
//                }
//            }

            //区域
            val area = view.findViewById<TextView>(R.id.tv_area)
            area.setOnClickListener {
                type = "区域"
                if(para.isNotEmpty()) {
                    lifecycleScope.launch {
                        vm.getCarType(para,type).collect{
                            carTypeAdapter.submitData(it)
                        }
                    }
                }
            }

            //车牌号
            val carNum = view.findViewById<TextView>(R.id.tv_num)
            carNum.setOnClickListener {
                type = "车牌号"
                lifecycleScope.launch {
                    //过滤车牌号
                    carTypeAdapter.submitData(datas.filter {
                        it.licensePlate == para
                    })
                }
            }
            popupWindow.contentView = view
            popupWindow.showAtLocation(binding.ivShai,Gravity.RIGHT,0,0)

        }

        //搜索
        binding.sv.setOnQueryTextListener(object : androidx.appcompat.widget.SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                //输入内容赋值为 para
                para = query!!
                lifecycleScope.launch {
                    vm.getCarType(query!!,"区域").collect{
                        carTypeAdapter.submitData(it)

                        if(type == "区域") {
                            vm.getCarType(query!!,type).collect{
                                carTypeAdapter.submitData(it)
                            }
                        }else {
                            carTypeAdapter.submitData(datas.filter {
                                it.licensePlate == para
                            })
                        }
                    }
                }
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                return true
            }
        })

        //进入车位编辑
        carTypeAdapter.setOnItemLongClickListener(object :OnItemLongClickListener{
            override fun onItemLongClick(item: CarTypeEntityItem): Boolean {
                ARouter.getInstance().build("/app/XiuParkActivity")
                    .withInt("pid",item.id)
                    .navigation()
                return true
            }

        })
    }

    //生命周期问题，刷新列表
    override fun onResume() {
        super.onResume()
        lifecycleScope.launch {
            vm.getCarType("标准车位",type).collect() {
                datas = it
                carTypeAdapter.submitData(it)
            }

        }
    }

    override fun intiData() {


    }
}